package com.verisette.ncos.service.master.bizunit;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;

import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bizunit.CommonBusinessUnit;
import com.verisette.ncos.common.CommonUtil;
import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.dao.AuditLogDAO;
import com.verisette.ncos.persistence.domain.CustomLogAuditLog;

import com.verisette.ncos.web.master.bean.AuditLogSearchBean;

public class InquiryAuditLogBU extends CommonBusinessUnit {

	private Logger logger = Logger.getLogger(InquiryAuditLogBU.class);
	
	private AuditLogDAO auditLogDAO;
	@Required
	@Autowired
	public void setAuditLogDAO(AuditLogDAO auditLogDAO) {
		this.auditLogDAO = auditLogDAO;
	}
	
	@Override
	public void execute(ProcessContext processContext) throws ProcessException {
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": execute" );
		
		AuditLogSearchBean searchBean = (AuditLogSearchBean)processContext.getValueObject();
		List<CustomLogAuditLog> auditLogList = null;
		
		logger.debug("1.Inquiry Audit Log in LOG_AUDIT_LOG.");
		
		Date fromDate = null;
		Date toDate = null;
		if(searchBean.getFromDate() != null && !"".equals(searchBean.getFromDate()) && searchBean.getToDate() != null && !"".equals(searchBean.getToDate())){
			// Convert Date String to Date
			fromDate = CommonUtil.convertDateTimeStrFormatToDate(searchBean.getFromDate() + " 00:00:001");
			toDate = CommonUtil.convertDateTimeStrFormatToDate(searchBean.getToDate() + " 23:59:599");
			
		}
		
		Integer uspId = null;
		if(searchBean.getUspId() != null && !"".equals(searchBean.getUspId()))
			uspId = Integer.parseInt(searchBean.getUspId());
		
		if(fromDate != null && toDate != null){
			logger.debug("--> fromDate : " + fromDate.toString());
			logger.debug("--> toDate : " + toDate.toString());
		}
		
		logger.debug("--> status : " + searchBean.getStatus());
		logger.debug("--> uspId : " + searchBean.getUspId());
		logger.debug("--> qsMatchSetID" + searchBean.getQsMatchSetID()); 
		
		if(searchBean.getQsMatchSetID() != null && !"".equals(searchBean.getQsMatchSetID()))
			auditLogList = auditLogDAO.inquiryAuditLog(searchBean.getQsMatchSetID(), processContext.getPaging()); // For Popup
		else
			auditLogList = auditLogDAO.inquiryAuditLog(fromDate, toDate, uspId, searchBean.getStatus(), processContext.getPaging()); // For Inquiry
		
		logger.debug("--> resultList size : " + auditLogList.size());
		processContext.setValueObject(auditLogList);
		
	}
	
}
